package com.example.shopping_web_back.service;

import com.example.shopping_web_back.mapper.OrderMapper;
import com.example.shopping_web_back.pojo.MonthlySalesNumDTO;
import com.example.shopping_web_back.pojo.MonthlySalesPriceDTO;
import com.example.shopping_web_back.pojo.OrdersOfGoods;
import com.example.shopping_web_back.pojo.PageBean;
import com.example.shopping_web_back.service.impl.OrderServiceImpl;
import com.example.shopping_web_back.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OrderService implements OrderServiceImpl {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void addOrder(OrdersOfGoods ordersOfGoods) {
        orderMapper.addOrder(ordersOfGoods);
    }

    @Override
    public void updateOrder(String arriveTime, String emailTime, String shippingTime, String deliveryTime, String status, String buyTime, String businessName, String goodsName, String customerName) {
        orderMapper.updateOrder(arriveTime, emailTime, shippingTime, deliveryTime, status, buyTime, businessName, goodsName, customerName);
    }

    @Override
    public void deleteOrder(OrdersOfGoods ordersOfGoods) {
        orderMapper.deleteOrder(ordersOfGoods);
    }

    @Override
    public PageBean<OrdersOfGoods> list(Integer pageNum, Integer pageSize) {
        //1.创建PageBean
        PageBean<OrdersOfGoods> pbOrders = new PageBean<>();
        Map<String, Object> map = ThreadLocalUtil.get();
        String customerName = (String) map.get("customer_name");

        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        List<OrdersOfGoods> ordersOfGoodsList = orderMapper.list(customerName);
        PageInfo<OrdersOfGoods> orders = new PageInfo<OrdersOfGoods>(ordersOfGoodsList);
        pbOrders.setTotal(orders.getTotal());
        pbOrders.setItems(orders.getList());
        return pbOrders;
    }

    @Override
    public PageBean<OrdersOfGoods> listBusiness(Integer pageNum, Integer pageSize) {
        //1.创建PageBean
        PageBean<OrdersOfGoods> pbOrders = new PageBean<>();
        Map<String, Object> map = ThreadLocalUtil.get();
        String businessName = (String) map.get("Business_name");

        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        List<OrdersOfGoods> ordersOfGoodsList = orderMapper.listBusiness(businessName);
        PageInfo<OrdersOfGoods> orders = new PageInfo<OrdersOfGoods>(ordersOfGoodsList);
        pbOrders.setTotal(orders.getTotal());
        pbOrders.setItems(orders.getList());
        return pbOrders;
    }

    @Override
    public double gotTotalSalesPrice(String businessName) {
        return orderMapper.gotTotalSalesPrice(businessName);
    }

    @Override
    public Integer gotTotalSalesNum(String businessName) {
        return orderMapper.gotTotalSalesNum(businessName);
    }

    @Override
    public List<MonthlySalesNumDTO> gotSixMonthSalesNum(String businessName) {
        return orderMapper.gotSixMonthSalesNum(businessName);
    }

    @Override
    public List<MonthlySalesPriceDTO> gotSixMonthSalesPrice(String businessName) {
        return orderMapper.gotSixMonthSalesPrice(businessName);
    }
}
